#include "../head/client.h"
#include <unistd.h>

int fds[2];

int main(int argc,char *argv[])
{
    pipe(fds);
    char *ip="192.168.7.111";
    char *port="36666";
    int sockfd=connect_net(ip,port);

    int epfd=epoll_create(1);
    TLVMsg msg;
    add_epoll(epfd,sockfd);
    add_epoll(epfd,fds[0]);
    add_epoll(epfd,STDIN_FILENO);
    struct epoll_event readytp[1024];  
    char buf[MAX_PATH_LEN]={0};
    
    /* recvfd(sockfd,&msg); */
    /* printf("%s",msg.value); */
    /* printf("\n"); */
    
    //interface();
    printf("----------------欢迎使用山寨客户端--------------------------------\n");
    printf("请输入  0  注册用户  or   1  登录用户\n");
    int type;
    scanf("%d",&type);
    if(type==0){
        transmit(sockfd,CMD_SIGN,strlen("nihao"),"nihao");
        register_account(sockfd);
    }else{
        transmit(sockfd,CMD_LOGIN,strlen("nihao"),"nihao");
        enter(sockfd);
    }
    fflush(stdout);
    while(1){
        int num=epoll_wait(epfd,readytp,1024,-1);
        for(int i=0;i<num;i++){
            int netfd=readytp[i].data.fd;
            if(netfd==STDIN_FILENO){ 
                bzero(buf,sizeof(buf));
                read(STDIN_FILENO,buf,sizeof(buf));
                process_characters(sockfd,buf,fds[1]);
            }
        }
    }
    return 0;

}
